package base;


import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 *  大整数乘法
 * 
 * */
public class BigNumberMultiplicative {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入乘数1：");
		String s1 = sc.nextLine();
		System.out.println("输入乘数2：");
		String s2 = sc.nextLine();
		String s = Multiplicative(s1, s2);
		System.out.println("结果为："+s);
	}
	
    private static String Multiplicative(String s1,String s2){
    	if(isNull(s1,s2)){
  		  return null;
  		}
  		if (s1.equals("0")||s2.equals("0")) {
  			return "0";
  		}
  		
  		if (s1.length()>s2.length()) {  //s2 被乘数 , s1乘数
  			String tmp = s1;
  			s1 = s2;
  			s2 = tmp;
  		}

    	return MultiplyMathod(s1,s2);
    }
	
	private static String MultiplyMathod(String s1,String s2){
		 int[] a=toArray(s1); //乘数
	     int[] b=toArray(s2); //被乘数
	      
	     int[] temp1=null;
	     int[] temp2=null;
	     
	     int enter=0;//进位
	     int result =0;
	     int count =1;
	     for(int i=b.length-1;i>=0;i--){  //乘数循环次数
	    	 temp1=new int[a.length+(++count)]; 
	    	 enter=0; //进位
	    	 for(int j=a.length-1;j>=0;j--){ //被乘数循环
	    		 result =a[j]*b[i]+enter;
	    		enter = result/10;
	    		if(result>=10){
	    			result=result%10;
	    		}
	    		 temp1[j+2] = result;
	                
	    	 }
	    	     temp1[1] = enter;
	             temp1[0] = 0;
	             
	            temp2 = addArray(temp1, temp2);
	     }
	     StringBuffer sb = new StringBuffer(32);
	        for(int j=0; j<temp2.length; j++){
	            if(temp2[j] == 0 && sb.length() == 0){
	                continue;
	            } else {
	                sb.append(temp2[j]);
	            }
	        }
	       
	        return sb.toString();
	}
	
	
	 //数组相加
	private static int[] addArray(int[] a, int[] b) {
		
		if (a == null)
			return b;
		if (b == null)
			return a;
		if (a.length != b.length) {
			// 判断两个数组的长度
			int[] temp = null;
			if (a.length < b.length) {
				temp = b;
				b = a;
				a = temp;
			}
			temp = new int[a.length];

			// 补齐 b的长度
			for (int i = a.length - 1, j = b.length - 1; i > 0; i--) {
				if (j > 0) {
					temp[i] = b[j--];
				} else {
					temp[i] = 0;
				}
			}
			b = temp;
		}

		int len = a.length;
		int c[] = new int[len + 1];// 存放结果

		int point = 0; // 进位标志

		for (int i = 0;i<len; i++) {
			int sum = a[len - 1 - i] + b[len - 1 - i] + point;
		if(sum>=10){
				c[i]= sum%10;
			}
			else
			c[i] = sum;
			point = sum / 10; // 进位数
		}
		c[len] = point;
      
		//交换数组前后位置
		for(int i=0,j=c.length-1;i<j;i++,j--){
			int temp =c[i];
			c[i]=c[j];
			c[j]=temp;
		}
		
		return c;
	}
	
	//字符串装换成数组
		private static int[] toArray(String str){
			int lenth =str.length();
			int[] arr =new int[lenth];
			for(int i=0;i<lenth;i++){
				arr[i]=str.charAt(i)-'0'; //将字符 一个个转换成数字存入数组中
			}
			
			return arr;
		}

	 //判断字符串是否为空，且是否满足正则
	private static boolean isNull(String str1, String str2) {
		if (str1.isEmpty() && str2.isEmpty() && !isMatcher(str1) && !isMatcher(str2))
			return true;
		return false;
	}
	
	  //判断是否满足正则表达式
		private static boolean isMatcher(String str){
			 Pattern p=Pattern.compile("[0-9]*");
		        Matcher isNum=p.matcher(str);
		        return isNum.matches();
		}
	
}
